package com.sr.security.handler;

import com.sr.enums.ResponseCode;
import com.sr.security.filter.LoginUserFilter;
import com.sr.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaochi
 * @date 2022/5/2 18:01
 * @desc AccessAuthenticationDeniedHandler 认证用户无权限访问资源时处理器
 */
@Slf4j
public class AccessAuthenticationDeniedHandler implements AccessDeniedHandler{

    private LoginUserFilter loginUserFilter;

    public AccessAuthenticationDeniedHandler(LoginUserFilter loginUserFilter){
        this.loginUserFilter = loginUserFilter;
    }

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        loginUserFilter.responseJson(response,R.error(ResponseCode.NOT_PERMISSION.getCode(),ResponseCode.NOT_PERMISSION.getMessage()));
    }
}
